package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysCoupon;
import com.ruoyi.project.system.domain.SysServiceOrder;
import com.ruoyi.project.system.service.ISysCouponService;
import com.ruoyi.project.system.service.ISysServiceOrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mobileUser/")
@Api(tags = "创建服务订单",description = "创建服务订单")
public class PayController extends BaseController {

    @Autowired
    private ISysServiceOrderService sysServiceOrderService;

    @Autowired
    private ISysCouponService sysCouponService;

    @Autowired
    public RedisTemplate redisTemplate;


    @RequestMapping(value = "/selectCouponList", method = RequestMethod.POST)
    @SignatureValidation
    public AjaxResult getUserCoupons(@RequestBody(required = false) SysCoupon sysCoupon){
        if(null==sysCoupon){
            return AjaxResult.error("参数错误");
        }
        if(null==sysCoupon.getTypeId()){
            return AjaxResult.error("分类id为空");
        }
        if(null==sysCoupon.getMoney()){
            return AjaxResult.error("订单价格必传");
        }
        sysCoupon.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysCoupon> list= sysCouponService.selectUseList(sysCoupon);
        return AjaxResult.success(list);
    }

    @RepeatSubmit
    @SignatureValidation
    @RequestMapping(value = "/createOrderService", method = RequestMethod.POST)
    public AjaxResult createOrderService(@RequestBody (required = false) SysServiceOrder sysServiceOrder){
        return sysServiceOrderService.createOrderService(sysServiceOrder);
    }

    @RequestMapping(value = "/payOrderService", method = RequestMethod.POST)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult payOrderService(@RequestBody SysServiceOrder sysServiceOrder){
        return sysServiceOrderService.payOrderService(sysServiceOrder);
    }

    @RequestMapping(value = "/sellectMyOrderList", method = RequestMethod.GET)
    @SignatureValidation
    public AjaxResult sellectMyOrderList( SysServiceOrder sysServiceOrder){
        startPage();
        sysServiceOrder.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        return AjaxResult.success(sysServiceOrderService.sellectMyOrderList(sysServiceOrder));
    }

    /**
     * 没有支付直接取消订单
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/cancelServiceOrder", method = RequestMethod.POST)
    @SignatureValidation
    public AjaxResult cancelServiceOrder(@RequestBody SysServiceOrder sysServiceOrder){
        SysServiceOrder order=sysServiceOrderService.selectSysServiceOrderById(sysServiceOrder.getOrderId());
        if(null==order){
            return AjaxResult.error("订单id错误!");
        }
        if("4".equals(order.getOrderStatus())){
            return AjaxResult.error("订单超时已取消");
        }
        if(!"0".equals(order.getOrderStatus())){
            return AjaxResult.error("订单状态错误!");
        }
        order.setOrderStatus("4");
        sysServiceOrderService.updateSysServiceOrder(order);
        return AjaxResult.success("操作成功!");
    }


    /**
     * 申请取消服务订单
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/applyServiceOrder", method = RequestMethod.POST)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult applyServiceOrder(@RequestBody SysServiceOrder sysServiceOrder){
        SysServiceOrder order=sysServiceOrderService.selectSysServiceOrderById(sysServiceOrder.getOrderId());
        if(null==order){
            return AjaxResult.error("订单id错误!");
        }
        if(null==sysServiceOrder.getReason()){
            return AjaxResult.error("原因不能为空");
        }
        if("1".equals(order.getOrderStatus())||"2".equals(order.getOrderStatus())){
            order.setCancelStatus("0");
            order.setReason(sysServiceOrder.getReason());
            sysServiceOrderService.updateSysServiceOrder(order);
            return AjaxResult.success("提交申请成功");
        }
        return AjaxResult.error("订单状态错误!");
    }

    /**
     * 用户确认完成服务
     * @param sysServiceOrder
     * @return
     */
    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
    @RepeatSubmit
    public AjaxResult confirmOrder(@RequestBody SysServiceOrder sysServiceOrder){
        return sysServiceOrderService.confirmOrder(sysServiceOrder);
    }

}
